#!/usr/bin/env python
# encoding: utf-8
# @author: Lin Han
# @contact: voldemort22@126.com
# @file: mongo_backup.py
# @time: 2021/5/18 14:25
# @desc:
import os
import time
from datetime import datetime

import pymongo

from configs import *
from crud import retrieve_all_mongo_values
from utils import save_json

while True:
    client = pymongo.MongoClient(MONGO_URL)

    db = client[MONGO_DATABASE]
    for i in db.list_collection_names(session=None):
        data = retrieve_all_mongo_values({}, collection_name=i)
        date = datetime.today().strftime("%Y-%m-%d")
        if not os.path.exists(f"data/{date}/"):
            os.makedirs(f"data/{date}/")
        save_json(data, f"data/{date}/{i}.json")

    time.sleep(3600 * 24)